package com.linzm.leetcode.mid.exercises3_20231218;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 316. 去除重复字母
 * 给你一个字符串 s ，请你去除字符串中重复的字母，使得每个字母只出现一次。需保证 返回结果的字典序最小（要求不能打乱其他字符的相对位置）。
 * 示例 1：
 * <p>
 * 输入：s = "bcabc"
 * 输出："abc"
 * 示例 2：
 * <p>
 * 输入：s = "cbacdcbc"
 * 输出："acdb"
 *
 * @Date: 2023/12/18 22:55
 * @User linziming
 */
public class Demo1_316 {
    public static void main(String[] args) {
        String str = "aacbd";
        Demo1_316 demo1316 = new Demo1_316();
        System.out.println(demo1316.removeDuplicateLetters(str));
    }

    public String removeDuplicateLetters(String s) {
        char[] charArray = s.toCharArray();
        // b c a b c a b c d
        // 1 2 3 4 5
        // 2 3 1 2 3
        // 指针1 向右寻找比当前值小的并最靠右的值
        // 指针2 向右寻找和当前值一样的值
        // <下标, 字符>
        Map<Character, Integer> map = new HashMap<>();
        for (int curIndex = 0; curIndex < charArray.length; curIndex++) {
            int smallIndex = 0;
            for (int moveIndex = curIndex + 1; moveIndex < charArray.length; moveIndex++) {
                if (charArray[curIndex] > charArray[moveIndex]) {
                    smallIndex = moveIndex;
                }
                // 没有向右寻找比当前值小的并最靠右的值
                if (charArray[curIndex] == charArray[moveIndex] && smallIndex == 0) {
                    map.put(charArray[moveIndex], moveIndex);
                }
                if (smallIndex != 0 && charArray[curIndex] == charArray[moveIndex] && (moveIndex - smallIndex > 0)) {
                    if (map.get(charArray[moveIndex]) != null) {
                        map.replace(charArray[moveIndex], moveIndex);
                    } else {
                        map.put(charArray[moveIndex], moveIndex);
                    }
                }
            }
        }
        List<Character> array = map.entrySet().stream().sorted(Map.Entry.comparingByValue()).map(Map.Entry::getKey).collect(Collectors.toList());
        StringBuilder stringBuilder = new StringBuilder();
        array.forEach(stringBuilder::append);
        return stringBuilder.toString();
    }

    public String demo(String s) {
        boolean[] vis = new boolean[26];
        int[] num = new int[26];
        for (int i = 0; i < s.length(); i++) {
            num[s.charAt(i) - 'a']++;
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (!vis[ch - 'a']) {
                while (sb.length() > 0 && sb.charAt(sb.length() - 1) > ch) {
                    if (num[sb.charAt(sb.length() - 1) - 'a'] > 0) {
                        vis[sb.charAt(sb.length() - 1) - 'a'] = false;
                        sb.deleteCharAt(sb.length() - 1);
                    } else {
                        break;
                    }
                }
                vis[ch - 'a'] = true;
                sb.append(ch);
            }
            num[ch - 'a'] -= 1;
        }
        return sb.toString();
    }
}
